<template>
<div class="co-store-page">
    <el-tabs editable @edit="onStoreMgrDialog" type="border-card" @tab-change="onTabChanged">
        <template v-for="item in stores" @key="item.id">
            <el-tab-pane :label="item.name">
                <store-panel :store-id="item.id" :mh="mh" :show="item.show" :is-rack="item.prop==1" :stores="stores" :show-op="true"></store-panel>
            </el-tab-pane>
        </template>
    </el-tabs>
</div>
</template>
<script>
import { computed, reactive, toRefs, ref } from 'vue-demi';
import { UiShowDialog } from '../uiframe/layout/components/CompFactory.vue';
import StoreMgrDialog from './store/StoreMgrDialog.vue';
import StorePanel from './store/StorePanel.vue';
import { UIMode } from '../uiframe/services/session';
import { useSocket } from '../uiframe/services/socket';

export default {
    components:{
        StorePanel
    },
    setup(props){
        let data = reactive({
            loading:false,
            stores:[],
            mh: UIMode.mh.value - (40)
        });
        const load = () => {
            useSocket().then(socket=>{
                socket.rpc('stores',{}).then(res=>{
                    data.stores = res.data;
                    data.stores.forEach((v,i)=>v.show = i==0)
                    if(data.stores.length > 0 ){
                        data.storeId = data.stores[0].id;
                    }
                })
            })
        }

        let methods = {
            onStoreMgrDialog:()=>{
                UiShowDialog(StoreMgrDialog, {payload:data.stores}).then(res=>{
                    console.log('StoreMgrDialog then res = ', res)
                    load();
                }).catch(res=>{
                    console.log('StoreMgrDialog catch res = ', res)
                })
            },
            onTabChanged:(i)=>data.stores.forEach((v, i1)=>v.show = i1==i)
        }

        load();
        return {
            ...toRefs(data),
            ...methods
        }
    }
}
</script>
<style lang="scss">
.co-store-page {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    top: 0;
    .el-tabs__header.is-top {
        padding: 0 10px 0 0;
        box-sizing: border-box;
        .el-tabs__item{
            .is-icon-close{display: none;}
        }
    }
    .el-tabs__content{padding:0;width:100%;}
}
</style>